<?php
/**
 * Created by PhpStorm.
 * User: XiaoBu <591538887@qq.com>
 * Date: 2017/10/11 0011
 * Time: 上午 11:57
 */

namespace Admin\Controller;

use Admin\Model\Banner;
use Admin\Model\Model;
use Common\Common\FileSystem;

class BannerController extends Controller
{
//    public function __construct()
//    {
//        parent::__construct();
//        $this->checkPermission('banner-edit');
//    }

    /**
     * banner列表
     */
    public function bannerList()
    {
        $model = M('banner');
        $data = $model->select();
        $this->assign('data', $data);
        $this->display('admin/banner/list');

    }

    /**
     * 添加banner
     */
    public function bannerAdd()
    {
        $model = M('banner');
        $data = $model->select();
        $this->assign('data', $data);
        $this->display('admin/banner/add');
    }

    /**
     * 添加banner数据
     */
    public function save()
    {
        if (!IS_POST){
            $this->error('请求错误');
        }
        $request = array_merge($_POST,$_FILES);
        $data = array_only($request,['id','link']);
        if(isset($request['src'])){
            $data['src'] = FileSystem::uploadFile('src');
        }
        if(Banner::insert($data))
        {
            $this->success('添加成功',U('list'),2);
        }else{
            $this->error('保存失败');
        }
    }

    /**
     * banner更新
     */
    public function update()
    {
        $id = $_GET['id'];
        $model = M('banner');

        $info = $model->where("id = $id")->find();
        $data = $model->select();
        $this->assign('id',$id);
        $this->assign('data', $data);
        $this->assign('info', $info);
        $this->display('admin/banner/list');

    }

    /**
     * banner更新数据
     */
    public function updates()
    {
        $model = M('banner');
        $id = $_POST['id'];
        $info = array_merge($_POST,$_FILES);
        $info['src'] = FileSystem::uploadFile('src');
        $model->where("id=$id")->save($info);
        $this->success('修改成功');

    }

    /**
     * 删除banner
     */
    public function delete()
    {
        $id = $_GET['id'];

        $model = M('banner');
        $model->where("id = $id")->delete();
        $this->success('删除成功',U('list'),2);

    }

}